package Date;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise01 {
    public static void main(String[] args) {
        String str = "abcdef";
        System.out.println("===交换前===");
        System.out.println(str);

        try {
            str = reverse(str, 1, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }

        System.out.println("===交换后===");
        System.out.println(str);
    }

    public static String reverse(String str,int start,int end){
        if(!(start >= 0 && end > start && end < str.length()) ){
            throw new RuntimeException("参数不正确!");
        }

        char chars[] = str.toCharArray();//把字符串转成数组
        char tmp =' ';
        while(start < end){
            tmp = chars[start];
            chars[start] = chars[end];
            chars[end] = tmp;
            start++;
            end--;
        }

        return new String(chars);
    }
}
